#statsNamespace.py
#Collects stats from files of namespace by startyear
#
#Health of Online Communities

import os
import collections
import sys
import multiprocessing
import datetime

def main():
	
	inputDir = sys.argv[1]
	outputDir = sys.argv[2]

	inputFileList = os.listdir(inputDir)

	workers = []
	
	for inputFileName in inputFileList:
		print inputFileName
		
		inputFilePath = inputDir+'/'+inputFileName
		outputFilePath = outputDir+'/'+inputFileName
		
		if os.path.isfile(inputFilePath):

			worker = Worker(inputFilePath, outputFilePath)
			workers.append(worker)
			worker.start()

	for worker in workers:
		worker.join()

class Worker(multiprocessing.Process):

	def __init__(self, inputFilePath, outputFilePath):
		multiprocessing.Process.__init__(self)
		self.inputFilePath = inputFilePath
		self.outputFilePath = outputFilePath

	def run(self):
		print timestamp() + " Starting processing for " + self.inputFilePath + " to " + self.outputFilePath + "\n"
		processFile(self.inputFilePath, self.outputFilePath)
		print timestamp() + " Finished processing for " + self.inputFilePath + " in " + self.outputFilePath + "\n"
			
def processFile(inputFilePath, outputFilePath):
	
	inputFileHandle = open(inputFilePath,"r")
	outputFileHandle = open(outputFilePath, 'a+')

	translate(inputFileHandle, outputFileHandle)
	
	inputFileHandle.close()
	outputFileHandle.close()

def translate(inputFileHandle, outputFileHandle):

	# Put your code here that contains the translation logic.
	# Read from the input file handle and write to the output file handle.
	# 
	# For (trivial) example:
	# 
	# for line in inputFileHandle:
	# 	outputFileHandle.write(line + ' Hmmm, interesting...')

	pass


def timestamp():
	return datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')

main()
